from __future__ import annotations

from typing import List, Optional

from sqlalchemy import ForeignKey, Integer, String
from sqlalchemy.dialects.postgresql import UUID
from sqlalchemy.orm import Mapped, mapped_column, relationship

from app.core.database import Base


class DictType(Base):
    __tablename__ = "sys_dict_type"

    dict_name: Mapped[str] = mapped_column(String(100), nullable=False, default="")
    dict_type: Mapped[str] = mapped_column(String(100), nullable=False, unique=True, index=True)
    status: Mapped[str] = mapped_column(String(1), nullable=False, default="0")
    remark: Mapped[Optional[str]] = mapped_column(String(500), nullable=True)
    create_by: Mapped[Optional[str]] = mapped_column(String(64), nullable=True)
    update_by: Mapped[Optional[str]] = mapped_column(String(64), nullable=True)

    data_items: Mapped[List["DictData"]] = relationship(
        "DictData",
        back_populates="dict_type_ref",
        cascade="all, delete-orphan",
    )


class DictData(Base):
    __tablename__ = "sys_dict_data"

    dict_sort: Mapped[int] = mapped_column(Integer, nullable=False, default=0)
    dict_label: Mapped[str] = mapped_column(String(100), nullable=False, default="")
    dict_value: Mapped[str] = mapped_column(String(100), nullable=False, default="")
    dict_type: Mapped[str] = mapped_column(String(100), nullable=False, index=True)
    css_class: Mapped[Optional[str]] = mapped_column(String(100), nullable=True)
    list_class: Mapped[Optional[str]] = mapped_column(String(100), nullable=True)
    is_default: Mapped[str] = mapped_column(String(1), nullable=False, default="N")
    status: Mapped[str] = mapped_column(String(1), nullable=False, default="0")
    remark: Mapped[Optional[str]] = mapped_column(String(500), nullable=True)
    create_by: Mapped[Optional[str]] = mapped_column(String(64), nullable=True)
    update_by: Mapped[Optional[str]] = mapped_column(String(64), nullable=True)
    dict_type_id: Mapped[UUID] = mapped_column(
        UUID(as_uuid=True),
        ForeignKey("sys_dict_type.id", ondelete="CASCADE"),
        nullable=False,
    )

    dict_type_ref: Mapped[DictType] = relationship("DictType", back_populates="data_items")
